import os
from pybs import version

from distutils.core import setup
from distutils.command.build import build
from distutils.command.install import install
from distutils.sysconfig import get_config_vars


from glob import glob
from os import listdir

class build_pybookshelf(build):
    def build_locales(self):
        for po in glob("po/*.po"):
            locale_code = os.path.splitext(os.path.basename(po))[0]
            mo_dir = os.path.join("po", locale_code, "LC_MESSAGES")
            if not os.path.exists(mo_dir):
                os.makedirs(mo_dir)
            os.system("msgmerge -o - %s po/messages.pot | msgfmt -c -o %s -" % (po, os.path.join(mo_dir, "pybookshelf.mo")))
    
    def build_path_py(self):
        prefix,  = get_config_vars('prefix')
        # open file `pybs/path.py' and replace ##PREFIX## with correct value
        f = open(os.path.join(self.build_lib, "pybs", "path.py"))
        p = f.read()
        p = p.replace("##PREFIX##", prefix)
        f.close()
        f = open(os.path.join(self.build_lib, "pybs", "path.py"), "w")
        f.write(p)
        f.close()
        pass

    def run(self):
        self.build_locales()
        build.run(self)
        self.build_path_py()

class install_pybookshelf(install):
    def run(self):
        install.run(self)

locale_files = []
for po in glob("po/*.po"):
    locale_code = os.path.splitext(os.path.basename(po))[0]
    mo_dir = os.path.join(locale_code, "LC_MESSAGES")
    locale_files.append((os.path.join("share", "locale", mo_dir), [os.path.join("po", mo_dir, "pybookshelf.mo")]))

plugins_files = []
plugins_base = os.path.join("lib", "pybookshelf", "plugins")
# add file plugins
pf = os.path.join("plugins", "file-plugins")
pb = os.path.join(plugins_base, "file-plugins")
for f in listdir(pf):
    if f == ".svn":
        continue
    fullpath = os.path.join(pf, f)

    if f.endswith(".py") and os.path.isfile(fullpath):
        plugins_files.append( (pb, [fullpath]) )
    elif os.path.isdir(fullpath):
        files = []
        for ff in listdir(fullpath):
            if (ff.endswith(".py") or ff.endswith(".glade")) and os.path.isfile(os.path.join(fullpath, ff)):
                files.append(os.path.join(fullpath, ff))

        plugins_files.append( (os.path.join(pb, f), files) ) 

setup(name="pybookshelf",
        version=version.__version__,
        description="Digital books organizer",
        long_description="Pybookshelf is a digital books organazer, provides web browser like interface to collection of book file.",
        author=version.__author__,
        author_email=version.__email__,
        maintainer="Sergei Stolyarov",
        maintainer_email="sergei@regolit.com",
        platforms=["Linux"],
        classifiers=[
            "Development Status :: 3 - Alpha",
            "Environment :: X11 Applications :: GTK",
            "Intended Audience :: End Users/Desktop",
            "License :: OSI Approved :: GNU General Public License (GPL)",
            #"Programming Language :: Python :: 2.5",
            "Operating System :: Unix",
            "Topic :: Text Processing :: Indexing"
            ],
        keywords=["digital book", "FictionBook2"],
        url="http://code.google.com/p/pybookshelf",
        download_url="",
        license="GPL v.2",
        packages=["pybs", "pybs.gui", "pybs.gui.browserparts"],#, "pybookshelf.services"],
        scripts=["pybookshelf"],
        cmdclass={
            'build': build_pybookshelf,
            'install': install_pybookshelf,
            },
        data_files=[
            #(os.path.join("share","man","man1"), ["pybookshelf.1"])
            (os.path.join("share", "pybookshelf", "xml"), glob("xml/*.xsl")),
            (os.path.join("share", "pybookshelf", "sql"), glob("sql/*.sql")),
            (os.path.join("share", "pybookshelf", "images"), glob("images/*.png")),
            (os.path.join("share", "pybookshelf", "html"), ["html/javascript.js"]),
            (os.path.join("share", "pybookshelf", "html", "templates"), glob("html/templates/*.html")),
            (os.path.join("share", "pybookshelf", "html", "css"), glob("html/css/*.css")),
            (os.path.join("share", "pybookshelf"), ["pybookshelf.glade"]),
            ] + locale_files + plugins_files
      )
